﻿using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

public class EditorAssetLoader : IAssetLoader
{
    private Dictionary<string, IAssetBundle> dicAsseetBundles;
    private Dictionary<UInt32, string> dicPrefab;
    private Dictionary<UInt32, string> dicAsset;
    private Dictionary<UInt32, string[]> dicSprite;
    private List<IAssetBundle> iabs;
    private UInt32 prefabResID;
    private UInt32 assetResID;
    public void Init()
    {
        prefabResID = 0;
        assetResID = 0;
        dicPrefab = new Dictionary<uint, string>();
        dicAsset = new Dictionary<uint, string>();
        dicSprite = new Dictionary<uint, string[]>();
        dicAsseetBundles = new Dictionary<string, IAssetBundle>();
        iabs = new List<IAssetBundle>();
    }

    public void Load()
    {
        
    }

    public uint LoadAsset(string bundleName, Action<uint> callback)
    {
        ++assetResID;
#if UNITY_EDITOR
        //string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(bundleName);
        ////object obj = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
        //dicSprite.Add(assetResID, assetPaths);
        //dicAsset.Add(assetResID, bundleName);
        //callback(assetResID);

        dicAsset.Add(assetResID, bundleName);

        if (dicAsseetBundles.ContainsKey(bundleName))
        {
            //引用次数+1
            IAssetBundle abRes = dicAsseetBundles[bundleName];
            abRes.AddReferenceCount();
            if (callback != null)
            {
                callback(assetResID);
            }
        }
        else
        {
            string[] depRes = AssetDatabase.GetAssetBundleDependencies(bundleName, true);
            if (depRes != null && depRes.Length > 0)
            {
                IAssetBundle iab = Create(bundleName, depRes, () =>
                {
                    if (callback != null)
                    {
                        IAssetBundle abRes = dicAsseetBundles[bundleName];
                        callback(assetResID);
                    }
                });
            }
            else
            {
                IAssetBundle iab = Create(bundleName, () =>
                {
                    if (callback != null)
                    {
                        callback(assetResID);
                    }
                });
            }
        }

#endif
            return assetResID;
    }

    public uint LoadPrefab(string bundleName, string assetName, Action<uint, IAssetBundle> callback)
    {
        ++prefabResID;
        GameObject go = null;
#if UNITY_EDITOR
        if (dicAsseetBundles.ContainsKey(bundleName))
        {
            IAssetBundle abRes = dicAsseetBundles[bundleName];
            //引用次数+1
            abRes.AddReferenceCount();
            if (callback != null)
            {
                callback(prefabResID, abRes);
            }
        }
        else
        {
            string[] depRes = AssetDatabase.GetAssetBundleDependencies(bundleName, true);
            if (depRes != null && depRes.Length > 0)
            {
                IAssetBundle iab = Create(bundleName, depRes, () =>
                {
                    if (callback != null)
                    {
                        IAssetBundle abRes = dicAsseetBundles[bundleName];
                        callback(prefabResID, abRes);
                    }
                });
            }
            else
            {
                IAssetBundle iab = Create(bundleName, () =>
                {
                    if (callback != null)
                    {
                        IAssetBundle abRes = dicAsseetBundles[bundleName];
                        callback(prefabResID, abRes);
                    }
                });
            }
        }
#endif

        dicPrefab.Add(prefabResID, bundleName);
        return prefabResID;

    }

    public void UnLoad(string bundleName, bool unloadImmediately = false)
    {
        throw new NotImplementedException();
    }

    public void UnLoadAsset(uint resID)
    {
        if (dicAsset.ContainsKey(resID) && dicSprite.ContainsKey(resID))
        {
            string bundleName = dicAsset[resID];
            dicAsset.Remove(resID);
            dicSprite.Remove(resID);
            Debug.LogWarning("Asset===》》count:" + dicAsset.Count);
            Debug.LogWarning("Sprite===》》count:" + dicSprite.Count);
            if (dicAsseetBundles.ContainsKey(bundleName))
            {
                IAssetBundle iab = dicAsseetBundles[bundleName];
                iab.ReleaseRes();
            }
        }
    }

    public void UnLoadPrefab(uint resID)
    {
        if (dicPrefab.ContainsKey(resID))
        {
            string bundleName = dicPrefab[resID];
            dicPrefab.Remove(resID);
            Debug.Log("resID:" + resID + ",bundleName:" + bundleName);
            if (dicAsseetBundles.ContainsKey(bundleName))
            {
                IAssetBundle iab = dicAsseetBundles[bundleName];
                iab.ReleaseRes();
            }
        }
    }

    public Sprite GetSpriteByResIDAndName(uint resID, string spriteName)
    {
#if UNITY_EDITOR
        if (dicSprite.ContainsKey(resID) && dicAsset.ContainsKey(resID))
        {
            int index = GetIndexByName(dicSprite[resID], spriteName);
            if(index < 0)
            {
                string bundleName = dicAsset[resID];
                Debug.LogError(bundleName + "不存在名为：" + spriteName + "的资源");
                return null;
            }
            else
            {
                Sprite sp = AssetDatabase.LoadAssetAtPath<Sprite>(dicSprite[resID][index]);
                return sp;
            }
            
        }
        else
        {
            Debug.LogError("resID:" + resID + ",不存在,请检查spriteName：" + spriteName + ",对应的AB资源是否已加载或者该资源是否设置AB");
            return null;
        }
#endif
        return null;
    }

    private int GetIndexByName(string[] strs, string name)
    {
        for (int i = 0; i < strs.Length; i++)
        {
            int startIndex = strs[i].LastIndexOf("/");
            int endIndex = strs[i].LastIndexOf(".") - startIndex;
            string str = strs[i].Substring(startIndex + 1, endIndex - 1);
            if (str == name)
            {
                return i;
            }
        }
        return -1;
    }

    public IAssetBundle GetIAssetBundle(string bundleName)
    {
        if (dicAsseetBundles.ContainsKey(bundleName))
        {
            return dicAsseetBundles[bundleName];
        }
        return null;
    }

    public void AddAssetBundle(string bundleName, IAssetBundle iab)
    {
        if (!dicAsseetBundles.ContainsKey(bundleName))
        {
            //Debug.Log("add:" + bundleName);
            dicAsseetBundles.Add(bundleName, iab);
        }
    }

    public IAssetBundle Create(string bundleName, string[] depRes, Action callback)
    {
        EditorAssetBundle iab = new EditorAssetBundle(this);
        iab.CreateRes(bundleName, callback);
        iabs.Add(iab);
        return iab;
    }

    public IAssetBundle Create(string bundleName, Action callback)
    {
        EditorAssetBundle iab = new EditorAssetBundle(this);
        iab.CreateRes(bundleName, callback);
        iabs.Add(iab);
        return iab;
    }

}
